package com.puboot.common.util;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author Leonidax
 * @date 2024/10/10 14:27
 * @description LocaleUtil
 */
@Component
public class LocaleUtil {

    private static MessageSource messageSource;

    public LocaleUtil(MessageSource messageSource) {
        LocaleUtil.messageSource = messageSource;
    }

    /**
     * 获取单个国际化翻译值
     */
    public String get(String msgKey) {
        try {
            return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return msgKey;
        }
    }

    public String language(){
        return LocaleContextHolder.getLocale().getLanguage();
    }
}
